#!/bin/bash

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

echo "========================================"
echo "   多仓库推送工具 (GitHub + Gitee)"
echo "========================================"
echo

# 检查是否有未提交的更改
if ! git status &>/dev/null; then
    echo -e "${RED}[错误] 当前目录不是 Git 仓库！${NC}"
    exit 1
fi

# 显示当前状态
echo -e "${BLUE}[信息] 当前仓库状态：${NC}"
echo
git status --short
echo

# 询问是否继续
read -p "是否继续提交并推送？(y/n): " continue
if [[ ! "$continue" =~ ^[Yy]$ ]]; then
    echo -e "${YELLOW}[取消] 操作已取消${NC}"
    exit 0
fi

echo
echo "----------------------------------------"

# 输入 commit message
read -p "请输入 commit 信息: " commit_msg
if [ -z "$commit_msg" ]; then
    echo -e "${RED}[错误] commit 信息不能为空！${NC}"
    exit 1
fi

echo
echo "----------------------------------------"

# 选择推送目标
echo "请选择推送目标："
echo "  1. 仅推送到 GitHub"
echo "  2. 仅推送到 Gitee"
echo "  3. 同时推送到 GitHub 和 Gitee (默认)"
echo
read -p "请选择 [1/2/3]: " push_target
push_target=${push_target:-3}

echo
echo "========================================"
echo "[步骤 1/3] 添加所有更改到暂存区..."
echo "========================================"
if ! git add .; then
    echo -e "${RED}[错误] git add 失败！${NC}"
    exit 1
fi
echo -e "${GREEN}[成功] 已添加所有更改${NC}"
echo

echo "========================================"
echo "[步骤 2/3] 提交更改..."
echo "========================================"
if ! git commit -m "$commit_msg"; then
    echo -e "${YELLOW}[警告] 没有需要提交的更改，或提交失败${NC}"
    echo
    read -p "是否强制推送现有提交？(y/n): " force_push
    if [[ ! "$force_push" =~ ^[Yy]$ ]]; then
        echo -e "${YELLOW}[取消] 操作已取消${NC}"
        exit 0
    fi
else
    echo -e "${GREEN}[成功] 提交成功${NC}"
fi
echo

echo "========================================"
echo "[步骤 3/3] 推送到远程仓库..."
echo "========================================"

github_failed=0
gitee_failed=0

# 根据选择推送
case $push_target in
    1)
        echo -e "${BLUE}[推送] 正在推送到 GitHub...${NC}"
        if ! git push github main; then
            echo -e "${RED}[错误] 推送到 GitHub 失败！${NC}"
            exit 1
        fi
        echo -e "${GREEN}[成功] 已推送到 GitHub${NC}"
        ;;
    2)
        echo -e "${BLUE}[推送] 正在推送到 Gitee...${NC}"
        if ! git push gitee main; then
            echo -e "${RED}[错误] 推送到 Gitee 失败！${NC}"
            exit 1
        fi
        echo -e "${GREEN}[成功] 已推送到 Gitee${NC}"
        ;;
    *)
        echo -e "${BLUE}[推送] 正在推送到 GitHub...${NC}"
        if ! git push github main; then
            echo -e "${RED}[错误] 推送到 GitHub 失败！${NC}"
            github_failed=1
        else
            echo -e "${GREEN}[成功] 已推送到 GitHub${NC}"
        fi
        echo
        
        echo -e "${BLUE}[推送] 正在推送到 Gitee...${NC}"
        if ! git push gitee main; then
            echo -e "${RED}[错误] 推送到 Gitee 失败！${NC}"
            gitee_failed=1
        else
            echo -e "${GREEN}[成功] 已推送到 Gitee${NC}"
        fi
        
        if [ $github_failed -eq 1 ] && [ $gitee_failed -eq 1 ]; then
            echo
            echo -e "${RED}[错误] 两个仓库推送都失败！${NC}"
            exit 1
        fi
        
        if [ $github_failed -eq 1 ]; then
            echo
            echo -e "${YELLOW}[警告] GitHub 推送失败，但 Gitee 推送成功${NC}"
        fi
        
        if [ $gitee_failed -eq 1 ]; then
            echo
            echo -e "${YELLOW}[警告] Gitee 推送失败，但 GitHub 推送成功${NC}"
        fi
        ;;
esac

echo
echo "========================================"
echo "          推送完成！"
echo "========================================"
echo

